package service

import (
	"common/biz"
	"common/logs"
	"common/utils"
	"context"
	"core/dao"
	"core/models/db"
	"core/repo"
	"fmt"
	"framework/msError"
	"hall/models/request"
	"hall/models/response"
)

type NewsService struct {
	newsDao *dao.NewsDao
	typeDao *dao.TypeDao
}

func (s *NewsService) NoticeAll(ctx context.Context) *[]db.Notice {
	return s.newsDao.NoticeAll(ctx)
}

func (s *NewsService) GetNotice(ctx context.Context, id uint) *db.Notice {
	return s.newsDao.GetNotice(ctx, id)
}

func (s *NewsService) EmailList(ctx context.Context, uid uint, page *request.PageMsg) (int64, *[]db.UserNotice) {
	return s.newsDao.EmailList(ctx, uid, page)
}

func (s *NewsService) GetEmail(ctx context.Context, uid uint, id uint) (*db.UserNotice, *msError.Error) {
	data := s.newsDao.GetEmail(ctx, id, uid)
	if data == nil || data.ID == 0 {
		logs.Error("[newsHandler]用户%d err:获取数据失败 req=%v", uid, data)
		return nil, biz.RequestDataError
	}
	return data, nil
}

func (s *NewsService) EmailReceive(ctx context.Context, uid uint, id uint) *msError.Error {
	data, err := s.GetEmail(ctx, uid, id)
	if err != nil {
		return err
	}
	if data.ReceiveID == 0 || data.IsReceive < 1 {
		logs.Error("[newsHandler]用户%d EmailReceive err:当前邮件不可领取 req=%v", uid, data)
		return biz.RequestDataError
	}
	if data.IsReceive == 2 {
		logs.Error("[newsHandler]用户%d EmailReceive err:当前邮件已领取 req=%v", uid, data)
		return biz.ReceiveError
	}
	err = s.newsDao.EmailReceive(ctx, uid, data.ReceiveID, data.ID)
	if err != nil {
		return err
	}
	return nil
}

func (s *NewsService) EmailRemove(ctx context.Context, uid uint, req *request.NewsRemove) *msError.Error {
	switch req.Type {
	case 1: //单条
		data, err := s.GetEmail(ctx, uid, req.ID)
		if err != nil {
			return err
		}
		if data.IsReceive == 1 {
			return biz.NotReceiveRemoveError
		}
		errs := s.newsDao.EmailRemove(ctx, uid, req.ID)
		if errs != nil {
			logs.Error("[newsHandler]用户%d EmailRemove err:删除邮件失败 req=%v,err=%v", uid, req, errs)
			return biz.SqlError
		}
	case 2: //全部
		err := s.newsDao.EmailRemoveAll(ctx, uid)
		if err != nil {
			logs.Error("[newsHandler]用户%d EmailRemove err:批量删除邮件失败 req=%v,err=%v", uid, req, err)
			return biz.SqlError
		}
	}
	return nil
}

func (s *NewsService) Record(ctx context.Context, uid uint, req *request.Record) (count int64, data any) {
	switch req.Type {
	case 1: //材料
		types := s.typeDao.GetInfo(ctx, 11)
		c, d := s.newsDao.MongoMaterialLogList(ctx, uid, req.TypeID, req.PageMsg)
		var res []response.MaterialLog
		for _, v := range *d {
			ago := utils.DecimalIntByFloat(v.Ago)
			num := utils.DecimalIntByFloat(v.Num)
			after := utils.DecimalIntByFloat(v.After)
			if v.TypeID == types.ID {
				ago = utils.DecimalFloatDiv(ago, utils.DecimalIntByFloat(types.Val))
				num = utils.DecimalFloatDiv(num, utils.DecimalIntByFloat(types.Val))
				after = utils.DecimalFloatDiv(after, utils.DecimalIntByFloat(types.Val))
			}
			res = append(res, response.MaterialLog{
				Id:         v.Id.String(),
				UID:        v.UID,
				WID:        v.WID,
				TypeID:     v.TypeID,
				TypeName:   v.TypeName,
				TypeImg:    v.TypeImg,
				TypeModule: v.TypeModule,
				LinkID:     v.LinkID,
				LinkTable:  v.LinkTable,
				Ago:        ago,
				Num:        num,
				After:      after,
				Remarks:    v.Remarks,
				AddTime:    v.AddTime,
			})
		}
		count, data = c, res
		break
	case 2: //生产记录
		types := s.typeDao.GetInfo(ctx, 11)
		c, d := s.newsDao.MongoOutputList(ctx, uid, req.PageMsg)
		res := *d
		for k, v := range res {
			res[k].CardOutput = utils.DecimalFloatDiv(v.CardOutput, utils.DecimalIntByFloat(types.Val))
			res[k].EquipOneOutput = utils.DecimalFloatDiv(v.EquipOneOutput, utils.DecimalIntByFloat(types.Val))
			res[k].EquipTwoOutput = utils.DecimalFloatDiv(v.EquipTwoOutput, utils.DecimalIntByFloat(types.Val))
			res[k].Output = utils.DecimalFloatDiv(v.Output, utils.DecimalIntByFloat(types.Val))
			res[k].SyOutput = utils.DecimalFloatDiv(v.SyOutput, utils.DecimalIntByFloat(types.Val))
			res[k].TotalOutput = utils.DecimalFloatDiv(v.TotalOutput, utils.DecimalIntByFloat(types.Val))
		}
		count = c
		data = res
		break
	case 3: //赠送记录
		c, d := s.newsDao.MongoTransferLogList(ctx, uid, req.PageMsg)
		res := *d
		for k, v := range res {
			if v.TypeID == 11 {
				types := s.typeDao.GetInfo(ctx, v.TypeID)
				res[k].Num = utils.DecimalFloatByInt(utils.DecimalIntDiv(v.Num, types.Val))
				res[k].Fee = utils.DecimalFloatDiv(v.Fee, utils.DecimalIntByFloat(types.Val))
				res[k].OtherNum = utils.DecimalFloatByInt(utils.DecimalIntDiv(v.OtherNum, types.Val))
			}
			res[k].Phone = fmt.Sprintf("%v****%v", v.Phone[0:3], v.Phone[7:])
			res[k].OtherPhone = fmt.Sprintf("%v****%v", v.OtherPhone[0:3], v.OtherPhone[7:])
		}
		count = c
		data = res
		break
	case 4: //开启卡包记录
		count, data = s.newsDao.MongoOpenPackageLogList(ctx, uid, req.PageMsg)
		break
	case 5: //投资中心兑换记录
		types := s.typeDao.GetInfo(ctx, 11)
		c, d := s.newsDao.MongoInvestExchangeLogList(ctx, uid, req.PageMsg)
		res := *d
		for k, v := range res {
			if v.TypeID == types.ID {
				res[k].Num = utils.DecimalFloatByInt(utils.DecimalIntDiv(v.Num, types.Val))
				res[k].Fee = utils.DecimalFloatDiv(v.Fee, utils.DecimalIntByFloat(types.Val))
				res[k].Total = utils.DecimalFloatDiv(v.Total, utils.DecimalIntByFloat(types.Val))
			}
			if v.ConsumeTypeID == types.ID {
				res[k].ConsumeNum = utils.DecimalFloatDiv(v.ConsumeNum, utils.DecimalIntByFloat(types.Val))
			}
		}
		count = c
		data = res
		break
	case 6: //抽奖记录
		c, d := s.newsDao.MongoLotteryLogList(ctx, uid, req.PageMsg)
		res := *d
		for k, v := range res {
			if v.ConsumeTypeID == 11 {
				types := s.typeDao.GetInfo(ctx, 11)
				res[k].ConsumeNum = utils.DecimalFloatByInt(utils.DecimalIntDiv(v.ConsumeNum, types.Val))
			}
		}
		count = c
		data = res
		break
	case 7: //提现记录
		count, data = s.newsDao.MongoExtractLogList(ctx, uid, req.PageMsg)
		break
	}
	return
}

func (s *NewsService) AddNews(ctx context.Context, uid uint, title, content string) error {
	news := db.UserNotice{
		UID:     uid,
		Title:   title,
		Content: content,
	}
	return s.newsDao.AddNews(ctx, &news)
}

func NewNewsService(r *repo.Manager) *NewsService {
	return &NewsService{
		newsDao: dao.NewNewsDao(r),
		typeDao: dao.NewTypeDao(r),
	}
}
